import dto.Configure;

import java.util.Date;
import java.util.Objects;

/**
 * @author sk.z
 */
public class Main {

    private static String CUR_IP;

    public static void main(String[] args) throws InterruptedException {
        System.out.println("init . . .");

        //read config file "ddns-dynv6.ini"
        Configure configure = ReadConfigure.read();

        //show get wan ip website list
        WanIp.init(configure.getWebsiteList());
        CUR_IP = WanIp.check();
        WanIp.showSite();

        System.out.println("start now.");
        int i = 0;
        while (true) {
            String ip = WanIp.get();
            if (Objects.isNull(ip)) {
                Thread.sleep(configure.getInterval());
                continue;
            } else if (ip.equals(CUR_IP)) {
                if (i > 60) {
                    i = 0;
                    System.out.println(".");
                } else {
                    i++;
                    System.out.print(".");
                }
            } else {
                UpdateDns.send(configure.getZone(), configure.getToken());
                System.out.println("[" + new Date() + "] change ip from " + CUR_IP + " to " + ip);
                CUR_IP = ip;
                i = 0;
            }
            Thread.sleep(configure.getInterval());
        }
    }

}
